﻿using DevWeb.Biztalk.DeploymentSettings.Data.CodeFirst;
using DevWeb.Biztalk.DeploymentSettings.WebUI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DevWeb.Biztalk.DeploymentSettings.WebUI.Controllers
{
    public class UserController : BaseController
    {
        // GET: User
        public ActionResult Index()
        {
            var users = (from c in Context.UserEnvironments.Include("UserEnvironmentRights").OrderBy(p => p.Username)
                         select new UserViewModel { Id = c.Id, Username = c.Username }).ToList();




          
            return View(users);
        }


        // Create: User
        public ActionResult Create()
        {
            UserViewModel model = new UserViewModel();

            return View(model);
        }

        // Create: User
        public ActionResult Details(int Id)
        {
            var user = Context.UserEnvironments.Include("UserEnvironmentRights").Include("UserEnvironmentRights.TargetEnvironment").Where(p => p.Id.Equals(Id)).SingleOrDefault();

            var model = new UserViewModel() { Username = user.Username, Id = user.Id, UserRights = new List<UserRightsViewModel>() };

            foreach (var right in user.UserEnvironmentRights)
            {
                UserRightsViewModel itemtoadd = new UserRightsViewModel() { Id = right.Id, Environment = right.TargetEnvironment.Name, EnvironmentID = right.TargetEnvironmentID, Right = right.Right };
                model.UserRights.Add(itemtoadd);
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(UserViewModel model)
        {
            if (ModelState.IsValid)
            {


                using (var trans = Context.GetDatabase().BeginTransaction())
                {

                    UserEnvironment user = new UserEnvironment() { Username = model.Username };
                    Context.UserEnvironments.Add(user);
                     Context.SaveChanges(System.Web.HttpContext.Current.User.Identity.Name);
                    List<UserEnvironmentRight> rights = new List<UserEnvironmentRight>();

                    var envs = Context.TargetEnvironments.ToArray();
                    for (int i = 0; i < envs.Count(); i++)
                    {
                        rights.Add(new UserEnvironmentRight() { TargetEnvironmentID = envs[i].Id, TargetEnvironment = envs[i], UserEnvironment = user, UserEnvironmentId = user.Id, Right = UserRight.None });
                    }

                    Context.UserEnvironmentRights.AddRange(rights);
                     Context.SaveChanges(System.Web.HttpContext.Current.User.Identity.Name);
                    trans.Commit();
                }

                return RedirectToAction("Index");
            }
            else
                return View(model);
            
            
        }
    }
}